/*
 * @component base.js
 * @description 基础方法库
 * @time 2019/3/13
 * @author JUSTIN
 */
export function delay(time = 300) {
  return new Promise(resolve => setTimeout(() => resolve(time), time));
}

/**
 * 根据数组对象排序 默认升序
 * @param {String} prop 属性值
 * @returns {number} 排序sort
 *
 */
export function compareProps(prop) {
  return (obj1, obj2) => {
    const val1 = obj1[prop];
    const val2 = obj2[prop];
    if (val1 < val2) {
      return -1;
    }
    if (val1 > val2) {
      return 1;
    }
    return 0;
  };
}

/**
 * 格式化money 适用于numberInput
 * @param {*} value number
 * @returns {function} 1,000
 */
export function formatMoney(value) {
  if (!value && value !== 0) {
    return null;
  }
  if (typeof value === 'number') {
    value = String(value);
  }
  return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

/**
 * 解析money 1,000,000 适用于numberInput
 * @param {*} value number
 * @returns {*} 1000
 */
export function parserMoney(value) {
  if (!value && value !== 0) {
    return null;
  }
  if (typeof value === 'number') {
    value = String(value);
  }
  const res = value.replace(/\$\s?|(,*)/g, '');
  if (Number.isNaN(Number(res))) {
    return value;
  }
  return Number(res);
}

/**
 * 对象转换成 key value数组
 * @param {object} obj 原对象
 * @returns {array} 从顶层到当前层级
 */
export function mapToArray(obj) {
  return Object.keys(obj).map(key => obj[key]);
}

/**
 * 序列化模式
 * @param {Number} commMode 模式
 * @returns {*} 从顶层到当前层级
 */
export function formatCommMode(commMode) {
  commMode = Number(commMode);
  if (commMode === 1) {
    return {
      name: '现金网',
      style: { backgroundColor: 'rgba(255, 204, 153, 1)' },
    };
  }
  if (commMode === 2) {
    return {
      name: '信用网',
      style: { backgroundColor: 'rgba(153, 204, 255, 1)' },
    };
  }
  return {
    name: '',
    style: { backgroundColor: 'rgba(228, 228, 228, 1)' },
  };
}

/**
 * 递归查询tree path
 * @param {Array} list 1
 * @param {String} value 2
 * @param {String} equalKey 比对key
 * @param {String} returnKey 返回key
 * @param {Boolean} returnIndex 是否返回index
 * @returns {*} 从顶层到当前层级
 */
export function getTreePathList(list, value, {
  equalKey = 'name',
  returnKey = 'uid',
  returnIndex = false,
} = {}) {
  for (let i = 0; i < list.length; i++) {
    const { children = [], [equalKey]: name, [returnKey]: uid } = list[i];
    const returnMap = returnIndex ? {
      [returnKey]: uid,
      index: i,
    } : uid;
    // 搜索忽略字母大小写
    if (name.toLowerCase() === value.toLowerCase()) {
      return [returnMap];
    }
    if (children && children.length) {
      const res = getTreePathList(children, value, {
        equalKey,
        returnKey,
        returnIndex,
      });
      if (res) {
        return [returnMap, res].flat();
      }
    }
  }
}

/**
 * 禁言设置天，时，分，拼接
 * @param {number} 时间
 */
export function unitTime(value, unit) {
  if (unit !== undefined) {
    if (Number(value) === 0) {
      return '永久';
    }
    const info = consts.CHATROOM_BAN_TIME.find(item => Number(item.value) === Number(unit));
    const title = _.isUndefined(info) ? '' : info.title;
    return value + title;
  }
}
